package com.rw.coco.data.data.mapper;

import com.rw.coco.api.data.model.CharacterModel;
import com.rw.coco.api.data.persistence.simple.SimpleCharacterMapper;
import com.rw.coco.data.data.bean.FileCharacterModel;
import com.rw.coco.data.data.util.BeanUtil;
import com.rw.tool.util.io.FileIOUtil;
import com.rw.tool.util.io.file.BeanFile;
import com.rw.tool.util.io.file.wrapper.FileQueryWrapper;
import com.rw.tool.util.json.JsonUtil;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class FileCharacterMapper extends SimpleCharacterMapper {
    private static final String CHARACTER_DIR_PATH = "\\character\\";

    private final BeanFile<FileCharacterModel> beanFile;

    public FileCharacterMapper(String workPath) {
        File file = new File(workPath, CHARACTER_DIR_PATH);
        FileIOUtil.mkDirs(file);
        this.beanFile = new BeanFile<>(file, FileCharacterModel.class);
    }

    private FileCharacterModel convert(CharacterModel data) {
        return JsonUtil.convertObj(data, FileCharacterModel.class);
    }

    private CharacterModel reconvert(FileCharacterModel data) {
        return JsonUtil.convertObj(data, CharacterModel.class);
    }

    private FileCharacterModel removeId(FileCharacterModel data) {
        return BeanUtil.removeIdProp(data, FileCharacterModel.class);
    }

    @Override
    public void insert(CharacterModel data) {
        FileCharacterModel model = convert(data);
        beanFile.insert(model);
        data.setCharacterId(model.getCharacterId());
    }

    @Override
    public void insertAll(Collection<CharacterModel> dataList) {
        List<FileCharacterModel> modelList = dataList.stream()
                .map(this::convert)
                .collect(Collectors.toList());

        beanFile.insertAll(modelList);
    }

    @Override
    public void delete(Serializable id) {
        beanFile.deleteById(id);
    }

    @Override
    public void deleteAll(CharacterModel data) {
        beanFile.delete(BeanUtil.getQueryWrapper(data));
    }

    @Override
    public void update(CharacterModel data) {
        FileCharacterModel characterModel = convert(data);
        beanFile.update(BeanUtil.getIdWrapper(characterModel), removeId(characterModel));
    }

    @Override
    public CharacterModel selectOne(Serializable id) {
        FileQueryWrapper queryWrapper = new FileQueryWrapper();
        queryWrapper.eq("characterId", id);
        return beanFile.selectOne(queryWrapper);
    }

    @Override
    public List<CharacterModel> selectList(CharacterModel data) {
        return beanFile.selectList(BeanUtil.getQueryWrapper(data))
                .stream()
                .map(this::reconvert)
                .collect(Collectors.toList());
    }

    @Override
    public List<CharacterModel> selectList(Collection<? extends Serializable> id) {
        return beanFile.selectList(id)
                .stream()
                .map(this::reconvert)
                .collect(Collectors.toList());
    }
}